package com.example.demo.practice.dashboardview;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.IBaseActivity;
import com.example.demo.practice.tool.ServerHttp;
import com.example.demo.practice.tool.SoundPlay;
import com.example.demo.practice.tool.TimeAction;
import com.hanks.htextview.HTextView;
import com.hanks.htextview.HTextViewType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Hydrometer extends IBaseActivity {

    private String code;
    private String company;
    private String address;

    private HTextView textView1;
    private HTextView textView2;
    private HTextView textView3;

    private  double PERCENT = 250;
    private ImageView ivIndex;
    private TextView tvRatio;
    private TimeAction timeAction;
    private double Start = 0;
    private MediaPlayer mp;

    private ImageView before;
    private ImageView next;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            setHydrometer();
            super.handleMessage(msg);
        }
    };
    private void setHydrometer() {
        Start = PERCENT;
        PERCENT = Random();
        if (PERCENT > 270){
            SoundPlay soundplay = new SoundTempPlay1();
            soundplay.play();
        }else if(PERCENT < 230){
            SoundPlay soundplay = new SoundTempPlay2();
            soundplay.play();
        }
        tvRatio.setText(PERCENT + "");
        ivRotate(Start,PERCENT);
    }

    public double Random(){
        int pro = new Random().nextInt(300);
        return pro>210?pro:Random();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hydrometer);
        ivIndex = (ImageView) findViewById(R.id.iv_index);
        tvRatio = (TextView) findViewById(R.id.tv_ratio);
        tvRatio.setText(PERCENT + "");
        before = (ImageView)findViewById(R.id.before);
        next = (ImageView)findViewById(R.id.next);
        ivRotate(Start,PERCENT);

        Intent intent = getIntent();
        code = intent.getStringExtra("code");
        setTitle("电压监测" + code);
        company = intent.getStringExtra("company");
        address = intent.getStringExtra("address");
        textView1 = (HTextView) findViewById(R.id.code);
        code = "编号:" + code;
        textView1.animateText(code);
        textView2 = (HTextView) findViewById(R.id.company);
        company = "单位:" + company;
        textView2.animateText(company);
        textView3 = (HTextView) findViewById(R.id.address);
        address = "地址:" + address;
        textView3.animateText(address);

        timeAction = new TimeAction(handler, 2000);
        timeAction.start();

        before.setOnClickListener(new ImageView.OnClickListener(){
            @Override
            public void onClick(View v){
                String[] codes=code.split("C");
                String codeshow = codes[1];
                int codeshows = Integer.valueOf(codeshow);
                if(codeshows > 0){
                    textView1.setAnimateType(HTextViewType.FALL);
                    textView2.setAnimateType(HTextViewType.FALL);
                    textView3.setAnimateType(HTextViewType.FALL);
                    codeshows--;
                    code = "编号:PXC" + codeshows;
                    textView1.animateText(code);
                    textView2.animateText(company);
                    textView3.animateText(address);
                }
            }
        });

        next.setOnClickListener(new ImageView.OnClickListener(){
            @Override
            public void onClick(View v){
                String[] codes=code.split("C");
                String codeshow = codes[1];
                int codeshows = Integer.valueOf(codeshow);
                if(codeshows >= 0){
                    textView1.setAnimateType(HTextViewType.FALL);
                    textView2.setAnimateType(HTextViewType.FALL);
                    textView3.setAnimateType(HTextViewType.FALL);
                    codeshows++;
                    code = "编号:PXC" + codeshows;
                    textView1.animateText(code);
                    textView2.animateText(company);
                    textView3.animateText(address);
                }
            }
        });
    }

    /**
     * 旋转动画
     * @param percent
     */
    private void ivRotate(double start,double percent) {
        start = start / 3;
        percent = percent / 3;
        double startOffset = start > 100 ? 100 : start;
        double percentOffset = percent > 100 ? 100 : percent;
        RotateAnimation rotateAnimation = new RotateAnimation(180 * ((int) startOffset / 100f), 180 * ((int) percentOffset / 100f),
                Animation.RELATIVE_TO_SELF, 0.748f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(2000);
        rotateAnimation.setFillAfter(true);
        ivIndex.startAnimation(rotateAnimation);
    }

    private class SoundTempPlay1 implements SoundPlay {

        @Override
        public void play() {
            mp = MediaPlayer.create(Hydrometer.this, R.raw.vhigh);
            mp.start();
        }
    }
    private class SoundTempPlay2 implements SoundPlay {

        @Override
        public void play() {
            mp = MediaPlayer.create(Hydrometer.this, R.raw.vlow);
            mp.start();
        }
    }

    @Override
    protected void onDestroy() {
        timeAction.stop();
        super.onDestroy();
        handler = null;
        mp=null;
        this.finish();
    }

    @Override
    protected void onPause() {
        timeAction.stop();
        super.onPause();
        handler = null;
        mp=null;
        this.finish();
    }

}
